import 'package:elevator/get/intl_keys.dart';
import 'package:elevator/model/alerm_info.dart';
import 'package:elevator/model/lift_info.dart';
import 'package:elevator/model/warn_info.dart';
import 'package:elevator/pages/home/all/detail_new/pages/errors/errors_view.dart';
import 'package:elevator/pages/home/all/detail_new/pages/errors/search/search_view.dart';
import 'package:elevator/pages/home/all/detail_new/pages/messages/messages_view.dart';
import 'package:elevator/pages/home/all/detail_new/pages/signs/signs_view.dart';
import 'package:elevator/pages/home/all/detail_new/pages/specs/specs_view.dart';
import 'package:elevator/pages/home/all/detail_new/pages/status/status_view.dart';
import 'package:elevator/utils/utils_logger.dart';
import 'package:elevator/widgets/globel_widget.dart';
import 'package:elevator/widgets/spirit/spirit_view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'detail_new_logic.dart';

class DetailNewPage extends StatefulWidget {

  DetailNewPage({Key? key}) :super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _DetailNewPageState();
  }

}

class _DetailNewPageState extends State<StatefulWidget>{
  late DetailNewLogic logic;

  _DetailNewPageState(){
    Get.delete<DetailNewLogic>();
    logic = Get.put(DetailNewLogic());
  }


  @override
  void initState() {
    debugPrint("DetailNewPage:initState");
    logic.register();
    super.initState();
  }


  @override
  void dispose() {
    debugPrint("DetailNewPage:dispose");
    logic.unregister();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery
        .of(context)
        .size;
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(
            Icons.navigate_before,
            size: 36,
          ),
          onPressed: () {
            Get.back();
          },
        ),
        iconTheme: IconThemeData(color: Colors.white),
        flexibleSpace: Container(
          decoration: const BoxDecoration(gradient: LinearGradient(colors:[
            Color(0xff0e274a),
            Color(0xff447986)
          ], begin: Alignment.centerLeft, end: Alignment.centerRight)),
        ),
        centerTitle: true,
        title:
        const Image(image: AssetImage("images/logo.png"), width: 100.0),
        actions: [
          IconButton(
            ///icon: const Icon(Icons.person,size: 36,),
            icon:Image.asset("images/person.png"),
            onPressed: () {logic.goToSettingPage();},
          )
        ],
      ),
      floatingActionButton: Obx(() {
        return Visibility(visible: logic.pageIndex.value == 2,
            child: FloatingActionButton(
              onPressed: () => logic.showDatePickDialog(),
              child: Icon(Icons.search),));
      }),
      body: SingleChildScrollView(child: Container(
        // width: double.infinity,
        // height: double.infinity,
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage('images/app_bg.png'),
            fit: BoxFit.cover,
          ),
        ),
        child: Column(
          children: [
            InkWell(
                child: Container(
                  height: 45,
                  color: Colors.white,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      Text(logic.buildInfo.getTag()),
                      Center(child: Text(logic.getBuildName())),
                      Opacity(opacity: 1,
                          child: Icon(Icons.details))
                    ],
                  ),
                ),
                onTap:(){
                  logic.gotoBuildDetail();
                }
            ),
            SizedBox(
              height: 70,
              child: GetBuilder<DetailNewLogic>(
                  id: logic.ID_LIFT_INFO,
                  builder: (DetailNewLogic controller) {
                    return SpiritComponent(
                      onTap: () {
                        logd('onTap home view');
                        // Get.to(DetailPage());
                        // Get.to(DetailNewPage());
                      },
                      onLongPress: () {
                        logd('onLongPress');
                      },
                      liftType: logic.liftInfo.liftType,
                      liftOpenType:logic.liftInfo.liftOpenType,
                      curStates: logic.liftInfo.currentState,
                      lLeftText: logic.liftInfo.liftName,
                      lRightText: logic.liftInfo.currentAddr,
                      widgets: getAlermInfoWidgets(logic.liftInfo.alermList),
                    );
                  }),
            ),
            Container(
              height: 50,
              child: ListView(
                scrollDirection: Axis.horizontal,
                children: builderLiftListView()
              ),
            ),
            Container(
              height: size.height - 170,
              child: PageView(
                controller: logic.pageController,
                physics: const NeverScrollableScrollPhysics(), // 禁止滑动
                children: [
                  SpecsPage(buildInfo:logic.buildInfo,liftInfo:logic.liftInfo),
                  StatusPage(buildInfo:logic.buildInfo,liftInfo:logic.liftInfo),
                  ErrorsPage(buildInfo:logic.buildInfo,liftInfo:logic.liftInfo),
                  SignsPage(buildInfo:logic.buildInfo,liftInfo:logic.liftInfo),
                  MessagesPage(buildInfo:logic.buildInfo,liftInfo:logic.liftInfo)
                ],
              ),
            )
          ],
        ),
      )),
    );
  }

  Widget tabContainer(checked, {required Widget child}) {
    return Container(
      padding: const EdgeInsets.all(4),
      child: Container(
        decoration: BoxDecoration(
          color: checked ? Colors.grey : null,
          borderRadius: BorderRadius.circular(10),

        ),
        child: child,
      ),
    );
  }

  builderLiftListView() {
    if(logic.user.psAuth!<=5){
      return [
        Opacity(opacity: 1,
          child: Obx(() {
            return tabContainer(logic.pageIndex.value == 0,
              child: TextButton(
                  onPressed: () => logic.changePageByIndex(0),
                  child: Text(IntlKeys.tabSpecs.tr, style: TextStyle(color: logic.pageIndex.value == 0?null:Colors.white),)),
            );
          }),),
        Opacity(opacity: 1,
          child: Obx(() {
            return tabContainer(logic.pageIndex.value == 1,
              child: TextButton(
                  onPressed: () => logic.changePageByIndex(1),
                  child: Text(IntlKeys.tabStatus.tr, style: TextStyle(color: logic.pageIndex.value == 1?null:Colors.white),)),
            );
          }),),
        Opacity(opacity: 1,
          child: Obx(() {
            return tabContainer(logic.pageIndex.value == 2,
              child: TextButton(
                  onPressed: () => logic.changePageByIndex(2),
                  child: Text(IntlKeys.tabError.tr, style: TextStyle(color: logic.pageIndex.value == 2?null:Colors.white),)),
            );
          }),),
        Opacity(opacity: 1,
          child: Obx(() {
            return tabContainer(logic.pageIndex.value == 3,
              child: TextButton(
                  onPressed: () => logic.changePageByIndex(3),
                  child: Text(IntlKeys.tabMsg.tr, style: TextStyle(color: logic.pageIndex.value == 3?null:Colors.white),)),
            );
          }),),
        Opacity(opacity: 1,
          child: Obx(() {
            return tabContainer(logic.pageIndex.value == 4,
                child: TextButton(
                    onPressed: () => logic.changePageByIndex(4),
                    child: Text(IntlKeys.tabAnalysis.tr, style: TextStyle(color: logic.pageIndex.value == 4?null:Colors.white),))
            );
          }),),
      ];
    }else{
      return [
        Opacity(opacity: 1,
          child: Obx(() {
            return tabContainer(logic.pageIndex.value == 0,
              child: TextButton(
                  onPressed: () => logic.changePageByIndex(0),
                  child: Text(IntlKeys.tabSpecs.tr, style: TextStyle(color: logic.pageIndex.value == 0?null:Colors.white),)),
            );
          }),),
        Opacity(opacity: 1,
          child: Obx(() {
            return tabContainer(logic.pageIndex.value == 1,
              child: TextButton(
                  onPressed: () => logic.changePageByIndex(1),
                  child: Text(IntlKeys.tabStatus.tr, style: TextStyle(color: logic.pageIndex.value == 1?null:Colors.white),)),
            );
          }),),
      ];
    }
  }

  getAlermInfoWidgets(List<AlermInfo> list) {
    var width = (Get.width - 20) / 6;
    List<Widget> listWidget = [];
    var size = list.length;

    for (int i = 0; i < size; i++) {
      var alarm = list[i];
      if (i != 0) {
        listWidget.add(
            Container(width: 1, height: 48, color: Colors.black,));
      }
      Color color = alarm.getBgColor();
      listWidget.add(
          SizedBox(
              width: width, child:
          Container(
              color: color, height: 48, child:
          Center(
              child: Text(alarm.getTag())))));
    }
    if (size < 3) {
      for (var i = size; i < 3; i++) {
        listWidget.add(
            Container(width: 1, height: 48, color: Colors.black,));
        listWidget.add(
            SizedBox(
                width: width, child:
            Container(width: 1, height: 48,)));
      }
    }
    return listWidget;
  }

  List<AlermInfo> getFakeList() {
    AlermInfo alarmInfo = AlermInfo.buildFormWarnInfo(WarnInfo(1,1,"sn","sn","sn",1,0));
    AlermInfo alarmInfo2 = AlermInfo.buildFormWarnInfo( WarnInfo(1,1,"fn","fn","fn",1,0));
    return [alarmInfo, alarmInfo2];
  }

}